﻿var c_TheForm;
var c_SubmitBtn;
var c_ResultDiv;
var c_ResultSeg;
var o_CurRes;
var o_CurSource;

function GetElements()
{
	c_TheForm = YAHOO.util.Dom.get("TheForm");
	c_SubmitBtn = YAHOO.util.Dom.get("SubmitBtn");
	c_ResultDiv = YAHOO.util.Dom.get("ResultDiv");
}

function SubmitSuccess(o)
{
	c_SubmitBtn.disabled = false;
	try
	{
		var data = YAHOO.lang.JSON.parse(o.responseText);
		
		if(data.IsError)
		{
			c_ResultDiv.innerText = data.Message;
		}
		else
		{
			o_CurSource = data.Source;
			BuildResult(data.Matches);
		}
	}
	catch(e)
	{
		c_ResultDiv.innerText = "解析响应时出错，请稍后再试。"+e.message;
	}
}

function SubmitFail(o)
{
	c_SubmitBtn.disabled = false;
	c_ResultDiv.innerText = "在连接服务器时发生错误，请稍后再试。";
}

function DoSubmit()
{
	c_SubmitBtn.disabled = true;
	try { c_ResultSeg.hide(); } catch(e) {}
	c_ResultDiv.innerHTML = "<img src='/Content/images/loading.gif'/>正在加载测试结果……";
	
	var callback =
	{
		success: SubmitSuccess,
		failure: SubmitFail,
		timeout: 30000
	};
	
	YAHOO.util.Connect.setForm(c_TheForm);
	YAHOO.util.Connect.asyncRequest("POST", "/Regex/TestSubmit/", callback, null);
}

function BuildResult(matches)
{
	o_CurRes = matches;
	if(matches.length == 0)
	{
		c_ResultDiv.innerText = "无任何匹配。";
		return;
	}
	
	var rd = document.createElement("div");
	
	var rod = document.createElement("div");
	rd.appendChild(rod);
	rod.innerHTML = "<div>共"+matches.length+"个匹配。</div>";
	rod.innerHTML += "<div><a href='javascript:fda();'>全部折叠</a> <a href='javascript:fdg();'>折叠到分组</a> <a href='javascript:fdc();'>折叠到捕获</a> <a href='javascript:ufa();'>全部展开</a></div>";
	
	for(var i = 0; i < matches.length; i++)
	{
		var m = matches[i];
		var rm = document.createElement("div");
		var rmt = document.createElement("div");
		var rmc = document.createElement("div");
		rm.appendChild(rmt);
		rm.appendChild(rmc);
		rd.appendChild(rm);
		
		rm.className = i % 2 ? "rm" : "rm-a";
		rmt.className = "rmt";
		rmc.className = "rmc";
		
		rmc.id = GetMatchId(i);
		YAHOO.util.Event.addListener(rmt, "click", new Function("sh('"+rmc.id+"')"));
		
		var mShowSegId = "m_" + i;
		rmt.innerHTML = "<img id='" + mShowSegId + "' src='/Content/images/newin.gif' alt='预览此片段'/>&nbsp;&nbsp;";
		rmt.innerHTML += "Match " + i + " [Index: " + m.Index + ", Length: " + m.Length + "]";
		rmc.innerHTML = m.Value;
		
		YAHOO.util.Event.addListener(mShowSegId, "click", ShowSeg, mShowSegId);

		for(var j = 0; j < m.Groups.length; j++)
		{
			var g = m.Groups[j];
			var rg = document.createElement("div");
			var rgt = document.createElement("div");
			var rgc = document.createElement("div");
			rg.appendChild(rgt);
			rg.appendChild(rgc);
			rmc.appendChild(rg);
			
			rg.className = j % 2 ? "rg" : "rg-a";
			rgt.className = "rgt";
			rgc.className = "rgc";
			
			rgc.id = GetGroupId(i,j);
			YAHOO.util.Event.addListener(rgt, "click", new Function("sh('"+rgc.id+"')"));
			
			var gShowSegId = "g_" + i + "_" + j;
			rgt.innerHTML = "<img id='"+ gShowSegId + "' src='/Content/images/newin.gif' alt='预览此片段'/>&nbsp;&nbsp;";
			rgt.innerHTML += "Group " + j + " [Index: " + g.Index + ", Length: " + g.Length + "] (" + g.Name + ")";
			rgc.innerHTML = g.Value;
			
			YAHOO.util.Event.addListener(gShowSegId, "click", ShowSeg, gShowSegId);
			
			for(var k = 0; k < g.Captures.length; k++)
			{
				var c = g.Captures[k];
				var rc = document.createElement("div");
				var rct = document.createElement("div");
				var rcc = document.createElement("div");
				rc.appendChild(rct);
				rc.appendChild(rcc);
				rgc.appendChild(rc);
				
				rc.className = k % 2 ? "rc" : "rc-a";
				rct.className = "rct";
				rcc.className = "rcc";
			
				rcc.id = GetCaptureId(i,j,k);
				YAHOO.util.Event.addListener(rct, "click", new Function("sh('"+rcc.id+"')"));

				var cShowSegId = "c_" + i + "_" + j + "_" + k;				
				rct.innerHTML = "<img id='" + cShowSegId + "' src='/Content/images/newin.gif' alt='预览此片段'/>&nbsp;&nbsp;";
				rct.innerHTML += "Capture " + k + " [Index: " + c.Index + ", Length: " + c.Length + "]";
				rcc.innerHTML = c.Value;

				YAHOO.util.Event.addListener(cShowSegId, "click", ShowSeg, cShowSegId);
			}
		}
	}
	
	c_ResultDiv.innerText = "";
	c_ResultDiv.innerHTML = "";
	c_ResultDiv.appendChild(rd);
	fdg();
}

function GetMatchId(i){return "rmd_m"+i;}
function GetGroupId(i,j){return GetMatchId(i)+"g"+j;}
function GetCaptureId(i,j,k){return GetGroupId(i,j)+"c"+k;}
function sh(id){var o=YAHOO.util.Dom.get(id);if(o.style.display == 'none') uf(o); else fd(o);}
function fd(o){o.style.display = "none";}
function uf(o){o.style.display = "block";}
function fda()
{
	for(var i in o_CurRes)
		fd(YAHOO.util.Dom.get(GetMatchId(i)));
}
function ufa()
{
	for(var i in o_CurRes)
	{
		uf(YAHOO.util.Dom.get(GetMatchId(i)));
		for(var j in o_CurRes[i].Groups)
		{
			uf(YAHOO.util.Dom.get(GetGroupId(i,j)));
			for(var k in o_CurRes[i].Groups[j].Captures)
				uf(YAHOO.util.Dom.get(GetCaptureId(i,j,k)));
		}
	}
}
function fdg()
{
	for(var i in o_CurRes)
	{
		uf(YAHOO.util.Dom.get(GetMatchId(i)));
		for(var j in o_CurRes[i].Groups)
			fd(YAHOO.util.Dom.get(GetGroupId(i,j)));
	}
}
function fdc()
{
	for(var i in o_CurRes)
	{
		uf(YAHOO.util.Dom.get(GetMatchId(i)));
		for(var j in o_CurRes[i].Groups)
		{
			uf(YAHOO.util.Dom.get(GetGroupId(i,j)));
			for(var k in o_CurRes[i].Groups[j].Captures)
				fd(YAHOO.util.Dom.get(GetCaptureId(i,j,k)));
		}
	}
}
function enc(t)
{   
	return t.replace(/&/g,'&amp;').replace(/\"/g,'&quot;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
}
function ShowSeg(e,oid)
{
	var ss = oid.split('_');
	var i,j,k;
	var o;
	var title = "结果片段 ";
	
	if(ss[0] == 'm')
	{
		i = parseInt(ss[1]);
		o = o_CurRes[i];
		title += "Match " + i;
	}
	else if(ss[0] == 'g')
	{
		i = parseInt(ss[1]);
		j = parseInt(ss[2]);
		o = o_CurRes[i].Groups[j];
		title += "Match " + i + " Group " + j;
	}
	else if(ss[0] == 'c')
	{
		i = parseInt(ss[1]);
		j = parseInt(ss[2]);
		k = parseInt(ss[3]);
		o = o_CurRes[i].Groups[j].Captures[k];
		title += "Match " + i + " Group " + j + " Capture " + k;
	}
	
	var body = "<div class='inr'>" + enc(o_CurSource.substr(0, o.Index));
	body += "<span id='rslct' class='slct'>" + enc(o_CurSource.substr(o.Index, o.Length)) + "</span>";
	body += enc(o_CurSource.substr(o.Index + o.Length)) + "</div>";

	c_ResultSeg.setHeader(title);
	c_ResultSeg.setBody(body);
	
	YAHOO.util.Dom.get("rslct").scrollIntoView();
	
	c_ResultSeg.moveTo(
		YAHOO.util.Event.getPageX(e),
		YAHOO.util.Event.getPageY(e));
	c_ResultSeg.show();
	window.event.cancelBubble = true;
}

function init()
{
	GetElements();
	YAHOO.util.Event.addListener(c_SubmitBtn, "click", DoSubmit);
	c_ResultSeg = new YAHOO.widget.Panel("ResultSeg",
	{
		width:"320px",
		visible:false,
		constraintoviewport:true
	} );
	c_ResultSeg.render(document.body);
}

YAHOO.util.Event.onDOMReady(init);
